package com.demo.wcx.greendaodemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    private ListView listview;
    private MyAdapter myAdapter;
    private List<UserBean> userBeanList = new ArrayList<>();
    private ImageView add;
    private DBHelper dbHelper;
    private EditText searchContant;
    private TextView search;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listview = (ListView) findViewById(R.id.listview);
        searchContant = (EditText) findViewById(R.id.search_contant);
        search = (TextView) findViewById(R.id.search);
        dbHelper = DBHelper.getInstance(this);
        add = (ImageView) findViewById(R.id.add);

        myAdapter = new MyAdapter(this, userBeanList) {
            @Override
            public void del(int position, View view) {
                dbHelper.deleteNote(userBeanList.get(position));
                userBeanList.clear();
                userBeanList.addAll(dbHelper.loadAllUserbean());
                myAdapter.notifyDataSetChanged();
            }

            @Override
            public void edit(int position, View view) {
                Intent intent = new Intent(MainActivity.this, EditUserbeanActivity.class);
                intent.putExtra("userbean", userBeanList.get(position));
                startActivity(intent);
            }
        };
        listview.setAdapter(myAdapter);

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, AddUserbeanActivity.class);
                startActivity(intent);
            }
        });

        searchContant.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(searchContant.getText().toString().trim())) {
                    userBeanList.clear();
                    userBeanList.addAll(dbHelper.loadAllUserbean());
                    myAdapter.notifyDataSetChanged();
                }
            }
        });
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(searchContant.getText().toString().trim())) {
                    Toast.makeText(MainActivity.this, "请输入搜索内容！", Toast.LENGTH_SHORT).show();
                }else {
                    userBeanList.clear();
                    userBeanList.addAll(dbHelper.searchByAll(searchContant.getText().toString().trim()));
                    myAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        userBeanList.clear();
        userBeanList.addAll(dbHelper.loadAllUserbean());
        myAdapter.notifyDataSetChanged();
    }
}
